package com.travel.Controller;



import com.travel.Bean.Manager;
import com.travel.Config.Security_PasswordEncoder;
import com.travel.Dao.ManagerMapper;
import com.travel.JPA.ManagerJPA;
import com.travel.Service.Impl.ManagerServiceImpl;
import com.travel.util.AES;
import com.travel.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/adminPublic")
public class AdminPublicController {

    @Autowired
    private ManagerJPA managerJPA;
    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private ManagerServiceImpl managerService;

    @ApiOperation("添加管理员页面")
    @GetMapping("adminAddPage")
    public ModelAndView adminAddPage(){
        return new ModelAndView("public/add");
    }


    @ApiOperation("修改管理员信息页面")
    @GetMapping("adminEditPage")
    public ModelAndView adminEditPage(Integer id){
        ModelAndView modelAndView = new ModelAndView("public/edit");
        Manager manager = managerMapper.selManagerById(id);
        modelAndView.addObject("type","manager");
        modelAndView.addObject("admin",manager);
        return modelAndView;
    }

    @ApiOperation("添加管理员信息")
    @PostMapping("/saveManagerAdd")
    public R saveManaferAdd(Manager manager) throws Exception {

        if(managerJPA.findIdByUsername(manager.getUsername()) != null){
            return R.no(1,"用户已存在");
        }
        if(manager.getPassword() != null){
            String pwd = manager.getPassword();
            manager.setPassword(AES.encrypt(pwd, Security_PasswordEncoder.secretKey));
        }
        manager.setRole("ROLE_admin");
        if(managerJPA.save(manager).getId() != null)
            return R.ok(0);
        return R.no("创建失败");
    }


    @ApiOperation("管理员用户名查重")
    @PostMapping("reAdminUserName")
    public R reAdminUserName(String username){
        if(managerJPA.findIdByUsername(username) != null){
            return R.no(1,"用户已存在");
        }
        return R.ok(0);
    }


    @ApiOperation("保存修改的管理员信息")
    @PostMapping("saveManagerEdit")
    public R saveMenegerEdit(Manager manager) throws Exception {
        if(manager.getPassword() != null){
            String pwd = manager.getPassword();
            manager.setPassword(AES.encrypt(pwd, Security_PasswordEncoder.secretKey));
        }
        return R.ok(managerService.updateManagerEdit(manager));
    }

    /**
     * 删除管理
     *
     * @param id id
     * @return {@link R}
     */
    @ApiOperation("删除管理员")
    @PostMapping("DelAdmin")
    public R DelAdmin(Integer id){
        if (managerService.deleteManagerById(id)>0)
            return R.ok(0);
        return R.no(1);
    }

}
